#include <string>
#include <string.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include "Database.h"
#include "md5.h"
#include "LoginAccount.h"
#include "Admin.h"
#include "Citizen.h"
#include "Question.h"
#include "Topic.h"
#include "FeedBackForm.h"

void manageUserMenu(Admin admin){
    int choice, loop;
    string choiceStr;
    
    loop = 1;
    
    while(loop){
        system("clear");

        cout << "The user management menu" << endl << endl;
        cout << "1) Create a user" << endl;
        cout << "2) Delete a user" << endl;
        cout << "3) Modify a user details" << endl;
        cout << "4) Exit" << endl << endl;
        cout << "Enter a choice : ";
        getline(cin, choiceStr);
        choice = atoi(choiceStr.c_str());
        cout << endl;
        if(choice == 1){
            string userid, password, hash, name, date;

            cout << "Enter userid : ";
            getline(cin, userid);
            cout << "Enter password : ";
            getline(cin, password);
            hash = md5(password);
            cout << "Enter name : ";
            getline(cin, name);
            cout << "Enter date (DDMMYYYY) : ";
            getline(cin, date);
            cout << endl;
            admin.createCitizenRec(userid, hash, name, date);
            
            userid = "";
            password = "";
            name = "";
            date = "";
            getchar();
        }

        if(choice == 2){
            string userid;
            cout << "Enter userid : ";
            getline(cin, userid);
            cout << endl;
            admin.deleteCitizenRec(userid);
            userid = "";
            getchar();
        }

        if(choice == 3){
            string userid, newInfo, tmp, optStr;
            int option;
            cout << "Enter userid : ";
            getline(cin, userid);
            cout << endl;
            cout << "Select detail to be edited." << endl << endl;
            cout << "1) User ID" << endl;
            cout << "2) Password" << endl;
            cout << "3) Name" << endl;
            cout << "4) Date" << endl;
            cout << "5) Account Status" << endl << endl;
            cout << "Enter a option : ";
            getline(cin, optStr);
            option = atoi(optStr.c_str());
            cout << endl;
            switch(option)
            {
                case 1:
                    cout << "Enter new user ID : ";
                    getline(cin, newInfo);
                    break;
                case 2:
                    cout << "Enter new password : ";
                    getline(cin, tmp);
                    newInfo = md5(tmp);
                    break;
                case 3:
                    cout << "Enter new name : ";
                    getline(cin, newInfo);
                    break;
                case 4:
                    cout << "Enter new date (DDMMYYYY) : ";
                    getline(cin, newInfo);
                    break;
                case 5:
                    cout << "Change account status to (Active/Inactive) : ";
                    getline(cin, newInfo);
                    break;
            }
            cout << endl;
            admin.modifyCitizenRec(userid, option, newInfo);
            userid = "";
            newInfo = "";
            tmp = "";
            getchar();
        }

        if(choice == 4){
            loop = 0;
        }
    }
}

void manageTpQnMenu(){
    Topic tpObj;
    Question qnObj;
    int choice, qnNum, loop;
    string tpCode, tpDesc , question, qnNew, temp, qnNumstr, choiceStr;
    
    loop = 1;
    
    while(loop){
    system("clear");
    
    cout << "The topic and question management menu" << endl << endl;
    cout << "1)Create a new topic" << endl;
    cout << "2)Add new question to a topic" << endl;
    cout << "3)Edit a question in a topic" << endl;
    cout << "4)Delete a question in a topic" << endl;
    cout << "5)Exit" << endl << endl;
    cout << "Enter a choice : ";
    getline(cin, choiceStr);
    choice = atoi(choiceStr.c_str());
    
    cout << endl;
            
    switch(choice)
    {
        case 1:
            cout << "Topic code for new topic (topic_code#): ";
            getline(cin, tpCode);
            cout << "Topic description for new topic : ";
            getline(cin, tpDesc);
            cout << endl;
            
            if(tpCode == "" || tpDesc == ""){
                cout << "Error in inputs" << endl;
            }else{
                tpObj.CreateTopic(tpCode, tpDesc);
            }
            getchar();
            break;
        case 2:
            cout << "Topic code for question to be in (topic_code#): ";
            getline(cin, tpCode);
            cout << "Question number : ";
            getline(cin, qnNumstr);
            qnNum = atoi(qnNumstr.c_str());
            cout << "Question : ";
            getline(cin, question);
            cout << endl;
            qnObj.insertQuestion(tpCode, qnNum, question);
            getchar();
            break;
        case 3:
            cout << "Topic code for question to be edited (topic_code#): ";
            getline(cin, tpCode);
            cout << "Question number to be edited : ";
            getline(cin, qnNumstr);
            qnNum = atoi(qnNumstr.c_str());
            cout << endl;
            
            temp = qnObj.retrieveQuestion(tpCode, qnNum);
            
            if(temp != ""){
                cout << "Current question is \"" << temp << "\"" << endl;
                cout << "New question is : ";
                getline(cin, qnNew);
                cout << endl;
                qnObj.editQuestion(tpCode,qnNum, qnNew);
            }else{
                cout << "No question in the system" << endl;;
            }
            getchar();
            break;
        case 4:
            cout << "Topic code for question to be deleted (topic_code#): ";
            getline(cin, tpCode);
            cout << "Question number to be deleted : ";
            getline(cin, qnNumstr);
            qnNum = atoi(qnNumstr.c_str());
            cout << endl;
            temp = qnObj.retrieveQuestion(tpCode, qnNum);
            
            if(temp != ""){
                qnObj.deleteQuestion(tpCode, qnNum);
            }else{
                cout << "No question in the system";
            }        
            getchar();
            break;
        case 5:
            loop = 0;
            break;
    }
    tpCode = "";
    tpDesc = "";
    question = "";
    qnNew = "";
    temp = "";
    qnNumstr = "";
    }
}

void viewFBSummaryMenu(){
    FeedBackForm FBobj;
    Topic tpObj;
    string topicCode, choiceStr;
    stringstream ss;
    int choice, loop, amtOftp;
    
    loop = 1;
    
    while(loop){
        system("clear");

        cout << "FeedBack Form Summary Menu" << endl << endl;
        cout << "1) View result by topic_code" << endl;
        cout << "2) View summary" << endl;
        cout << "3) Exit" << endl << endl;
        cout << "Enter a choice : ";
        getline(cin, choiceStr);
        choice = atoi(choiceStr.c_str());
        cout << endl;
        switch(choice)
        {
            case 1:
                cout << "Enter topic code of result to be viewed (topic_code#) :";
                getline(cin, topicCode);
                cout << endl;
                FBobj.viewTopicSummary(topicCode);
                getchar();
                break;
            case 2: 
                amtOftp = tpObj.getAmtOfTopic();
                for(int i = 0; i < amtOftp ; i++){
                    ss << "topic_code" << i+1;
                    topicCode = ss.str();
                    FBobj.viewTopicSummary(topicCode);
                    ss.str("");
                    cout << endl;
                }
                getchar();
                break;
            case 3:
                loop = 0;
                break;
        }
        topicCode = "";
    }
}

int main()
{
    
    string inputIDstr, inputPasswdstr, PasswdHash;
    int inputID;
    
    cout << "Welcome to Online FeedBack system!" << endl << endl;
    cout << "Please enter your Account ID and Password to login." << endl << endl;
    cout << "Account ID : ";
    getline(cin, inputIDstr);
    cout << endl;
    cout << "Password : ";
    getline(cin, inputPasswdstr);
    
    cout << endl;
    
    inputID = atoi(inputIDstr.c_str());
    
    PasswdHash = md5(inputPasswdstr);
    
    LoginAccount testAcc(inputID, PasswdHash);
    
    if(testAcc.checkCred()){
        if(testAcc.getUsrType() == "Citizen"){
            Citizen citizenObj;
            int choice, loop;
            Topic tpObj;
            FeedBackForm FBobj;
            string topicCode, choiceStr;
            
            citizenObj.setfields(inputID);
            
            loop = 1;
            
            while(loop){
                system("clear");            

                cout << "Weclome to the citizen menu, user " << inputID << endl << endl;
                cout << "1) Choose topic to do feedback form" << endl;
                cout << "2) Exit" << endl << endl;
                cout << "Enter a choice : ";
                getline(cin, choiceStr);
                choice = atoi(choiceStr.c_str());
                cout << endl;
                switch(choice)
                {
                    case 1:
                        system("clear");
                        cout << "The list of topics in the system" << endl << endl;

                        tpObj.showTopic();

                        cout << endl;
                        cout << "Enter the topic code(topic_code#) : ";
                        getline(cin, topicCode);

                        system("clear");          

                        FBobj.doFeedBackForm(topicCode, citizenObj.getID());
                        cout << endl;
                        cout << "Returning to main menu" << endl;
                        getchar();
                        break;
                    case 2:
                        loop = 0;
                        break;
                }
                topicCode = "";
            }
        }
        
        if(testAcc.getUsrType() == "Admin"){
            Admin admin;
            int choice, loop;
            string choiceStr;
            
            loop = 1;
            
            while(loop){
            
                system("clear");

                cout << "This is the admin menu" << endl << endl;
                cout << "1) Manage user accounts" << endl;
                cout << "2) Manage topics and questions" << endl;
                cout << "3) View feedback summary and results" << endl;
                cout << "4) Exit" << endl << endl;
                cout << "Enter a choice : ";
                getline(cin, choiceStr);
                choice = atoi(choiceStr.c_str());

                switch(choice)
                {
                    case 1:
                        manageUserMenu(admin);
                        break;
                    case 2:
                        manageTpQnMenu();
                        break;
                    case 3:
                        viewFBSummaryMenu();
                        break;
                    case 4:
                        loop = 0;
                        break;
                }
            }
            
        }
    }
    
    return 0;
}